<?php
class PEAR_Exception2 extends Exception
{
    protected $stack = array();

    public function __construct($message, $code = null, $exception = null)
    {
        parent::__construct($message, $code);
        if ($exception !== null) {
            if (is_array($exception)) {
                $this->stack = array_merge($this->stack, $exception);
            } elseif ($exception instanceof Exception) {
                array_push($this->stack, $exception);
            }
        }
    }
    
    public function __toString()
    {
        $msg = "Message: {$this->message}, Code: {$this->code}";
        if (count($this->stack) > 0) {
            foreach ($stack as $exception) {
                $msg .= "\nException: {$exception->getTraceAsString()}";
            }
        }
        return $msg;
    }
}
